//
//  Mind.m
//  iDev iPad Proto 2
//
//  Created by Matthew Canoy on 10/29/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "Mind.h"

@implementation Tap

@synthesize tapLocation = m_TapLocation;

- (CGPoint)tapLocation {
	return m_TapLocation;
}

- (void)setTapLocation:(CGPoint)location {
	m_TapLocation = location;
}

@end


@implementation Mind

@synthesize vSounds = m_vSounds;
@synthesize vTextures = m_vTextures;
@synthesize vParticleEmitters = m_vParticleEmitters;
@synthesize vProfiles = m_vProfiles;
@synthesize vObjects = m_vObjects;
@synthesize vStrings = m_vStrings;
@synthesize tap = m_Tap;
@synthesize tiPreviousTime = m_tiPreviousTime;
@synthesize tiCurrentTime = m_tiCurrentTime;
static Mind* instance = nil;



+ (Mind *)mind { 
	if ( instance == nil ) { 
		instance = [[self alloc] init];
	} 
	return instance;
}
- (void)NewTap:(CGPoint)location {
	if(m_Tap)
	{
		//Something is wrong here.
		//this was supposed to be nulled.
	}
	
	Tap* newTap = [[Tap alloc] init];
	[newTap setTapLocation:location];
	m_Tap = newTap;
}

- (void)FlushTap {
	if(m_Tap) 
		free(m_Tap);
	
	m_Tap = nil;
}

- (Tap*)tap
{
	return m_Tap;
}

- (void)InitialSetup {
//	GLfloat verts[] = {
//		-0.5f, -0.5f,
//		0.5f, -0.5f,
//		-0.5f,  0.5f,
//		0.5f,  0.5f,
//	};
	
	GLfloat verts[] = {
		-1.0f, -1.5f,
		1.0f, -1.5f,
		-1.0f,  1.5f,
		1.0f,  1.5f,
	};
	
	for (int i = 0; i < 8; ++i) {
		spriteVertices[i] = verts[i];
	}
	
	m_vSounds = [[Vector alloc] init];
	m_vTextures = [[Vector alloc] init];
	m_vParticleEmitters = [[Vector alloc] init];
	m_vProfiles = [[Vector alloc] init];
	m_vObjects = [[Vector alloc] init];
	m_vStrings = [[Vector alloc] init];
	
}

- (GLfloat*)GetSpriteVertices {
	return spriteVertices;
}

- (float)DeltaTime
{
	m_tiCurrentTime = CFAbsoluteTimeGetCurrent();
	float fDeltaTime = m_tiCurrentTime - m_tiPreviousTime;
	m_tiPreviousTime = m_tiCurrentTime;
	
	return fDeltaTime;
}


#define screenWidth 704.0f
#define screenWidthDiv2 352.0f
#define	screenHeight 960.0f
#define	screenHeightDiv2 480.0f
- (void)setSpriteVerts:(CGPoint)point
{
	//spriteVertices[0] = (point.x - screenWidthDiv2) / (screenWidthDiv2)) - .5f;
//	spriteVertices[1] = (point.y - screenHeightDiv2) / (screenHeightDiv2)) - .5f;
//	spriteVertices[2] = (point.x - screenWidthDiv2) / (screenWidthDiv2)) + .5f;
//	spriteVertices[3] = (point.y - screenHeightDiv2) / (screenHeightDiv2)) - .5f;
//	spriteVertices[4] = (point.x - screenWidthDiv2) / (screenWidthDiv2)) - .5f);
//	spriteVertices[5] = (point.y - screenHeightDiv2) / (screenHeightDiv2)) + .5f);
//	spriteVertices[6] = (point.x - screenWidthDiv2) / (screenWidthDiv2)) + .5f);
//	spriteVertices[7] = (point.y - screenHeightDiv2) / (screenHeightDiv2)) + .5f);
	
	//pos / dimesion * 2 - 1
	CGPoint center;
	center.x = (point.x / screenWidthDiv2) * 2.0f - 1.0f;
	center.y = (point.y / screenHeightDiv2) * 3.0f - 1.5f;

	spriteVertices[0] = center.x - .5f;
	spriteVertices[1] = -1*center.y - .5f;
	spriteVertices[2] = center.x + .5f;
	spriteVertices[3] = -1*center.y - .5f;
	spriteVertices[4] = center.x - .5f;
	spriteVertices[5] = -1*center.y + .5f;
	spriteVertices[6] = center.x + .5f;
	spriteVertices[7] = -1*center.y + .5f;
}

@end
